﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Drawing;

using MyUtils.PInvoke.Structs;

namespace MyUtils.PInvoke
{
	public static class IconExtractor
	{
		public static Icon GetIconSmall(string fileName)
		{
			return GetIcon(fileName, false);
		}

		public static Icon GetIconLarge(string fileName)
		{
			return GetIcon(fileName, true);
		}

		public static Icon GetIcon(string fileName, bool large)
		{
			SHFILEINFO shinfo;
			PInvoke.SHGetFileInfo(fileName, 0, out shinfo, (uint)Marshal.SizeOf(typeof(SHFILEINFO)), PInvoke.SHGFI.Icon | (large ? PInvoke.SHGFI.LargeIcon : PInvoke.SHGFI.SmallIcon) /*| USEFILEATTRIBUTES*/);
			//The icon is returned in the hIcon member of the shinfo struct
			var icon = (Icon)Icon.FromHandle(shinfo.hIcon).Clone();
			PInvoke.DestroyIcon(shinfo.hIcon);
			return icon;
		}
	}
}
